package base.Chapter00_Structures.C07_Tree.BinaryTree;

/**
 * 二叉树遍历： 递归方式
 * 重点掌握遍历
 */
public class Tree {
    /**
     * 前序遍历:先打印当前
     * 先打印当前节点 --》左子树 --》右子树
     */
    public void preOrderEngodic(Node currentNode) {
        if (currentNode ==null) {
            return;
        }
        System.out.println(currentNode);
        preOrderEngodic(currentNode.left);
        preOrderEngodic(currentNode.right);
    }

    /**
     * 中序遍历：中间打印当前
     * 左子树 -- 当前节点 -- 右子树
     */
    public void inOrderEngodic(Node currentNode) {
        if (currentNode ==null) {
            return;
        }
        inOrderEngodic(currentNode.left);
        System.out.println(currentNode);
        inOrderEngodic(currentNode.right);
    }

    /**
     * 后序遍历：最后打印当前
     * 左子树 -- 右子树 -- 当前节点
     */
    public void postOrderEngodic(Node currentNode) {
        if (currentNode ==null) {
            return;
        }
        postOrderEngodic(currentNode.left);
        postOrderEngodic(currentNode.right);
        System.out.println(currentNode);
    }


    public static class Node {
        private int data;
        private Node left;
        private Node right;

        @Override
        public String toString() {
            return "Node{" +
                    "data=" + data +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }
    }

}
